package com.gym.core.common;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-6-9 下午5:39:40
 */
public class MessageFormatUtils {
	
     public static String formate(String source,Map<String,Object> params){
    	 if(StringUtils.isEmpty(source) || params == null || params.size() == 0){
    		 return source;
    	 }
    	 
    	 Map<String,Integer> keyIndexMap = new HashMap<String,Integer>();
    	 int i = 0;
    	 for(String key : params.keySet()){
    		 keyIndexMap.put(key, i++);
    	 }
    	 
    	 StringBuilder newStringPattern = new StringBuilder();
    	 Object[] newParams = new Object[params.size()];
    	 boolean valid = false;
    	 StringBuilder keyBuilder = new StringBuilder();
    	 for(char c : source.toCharArray()){
    		 if(c == '{' ){
    			 valid = true;
    			 newStringPattern.append(c);
    			 continue;
    		 } else  if(c == '}' ){
    			 valid = false;
    		 }
    		 
    		 if(valid == false) {
    			 if(keyBuilder.length() >0){
    				 int index = keyIndexMap.get(keyBuilder.toString());
    				 newStringPattern.append(index);
    				 newParams[index] = params.get(keyBuilder.toString());
    				 keyBuilder.setLength(0);
    			 }
    			 newStringPattern.append(c);
    		 } else {
    			 keyBuilder.append(c);
    		 }
    	 }
    	 return MessageFormat.format(newStringPattern.toString(), newParams);
     }
     
     enum ParseState {
 		/**
 		 * 起点
 		 */
 		bof,
 		/**
 		 * { 起点
 		 */
 		bracesStart,
 		/**
 		 * 变量开始
 		 */
 		variableStart,
 		/**
 		 * 普通文本
 		 */
 		plainTextStart,
 		/**
 		 * }结束
 		 */
 		bracesEnd,
 		/**
 		 * 结束
 		 */
 		eof
 	}

 	public static String formateFast(String str, Map<String, Object> params) {
 		if (str == null || str.length() == 0 || params == null
 				|| params.size() == 0) {
 			return str;
 		}
 		ParseState state = ParseState.bof;
 		StringBuilder buff = new StringBuilder();
 		StringBuilder keyBuff = new StringBuilder();
 		char[] cs = str.toCharArray();
 		for (char c : cs) {
 			switch (c) {
 			case '{':
 				switch (state) {
 				case bof:
 					break;
 				case bracesStart:
 					buff.append('{');
 					break;
 				}
 				state = ParseState.bracesStart;
 				break;
 			case '}':
 				switch (state) {
 				case bof:
 					buff.append('}');
 					state = ParseState.plainTextStart;
 					break;
 				case bracesStart:
 					buff.append('{');
 					buff.append('}');
 					break;
 				case bracesEnd:
 					buff.append('}');
 					break;
 				case variableStart:
 					String key = keyBuff.toString();
 					Object value = params.get(key);
 					if (value != null) {
 						buff.append(value == null ? "" : value);
 					} else {
 						buff.append('{');
 						buff.append(key);
 						buff.append('}');
 					}
 					keyBuff.setLength(0);
 					break;
 				}
 				state = ParseState.bracesEnd;
 				break;
 			default:
 				switch (state) {
 				case bracesStart:
 					keyBuff.append(c);
 					state = ParseState.variableStart;
 					break;
 				case bof:
 				case bracesEnd:
 				case plainTextStart:
 					buff.append(c);
 					state = ParseState.plainTextStart;
 					break;
 				case variableStart:
 					keyBuff.append(c);
 					break;
 				}
 			}

 		}
 		return buff.toString();
 	}
}
